// pages/my_space/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "", //用户id
    userInfo: {},
    hasUserInfo: false,
    height: 0, //以rpx为单位
    soultionNum: 0,
    noSolutionNum: 0
  },

  //获得页面高度并设置到data的height中
  getHeight(that) {
    wx.getSystemInfo({
      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let changeHeight = 750 / clientWidth;
        let height_ = clientHeight * changeHeight;
        that.setData({
          height: height_
        });
      }
    })
  },

  copyId() {
    var that = this;
    wx.setClipboardData({
      //准备复制的数据
      data: that.data.id,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that=this;
    var user_info = wx.getStorageSync('userInfo');
    var session_key_ = wx.getStorageSync('sessionKey');
    var user_id_ = wx.getStorageSync('userId');

    //1.获得用户信息并放到data中
    if (user_info != "" && session_key_ != "") {
      this.setData({
        hasUserInfo: true,
        userInfo: user_info,
        id: user_id_
      })
    }

    //2.获得页面高度
    this.getHeight(this);

    //3.后端，获得解答问题和未解答问题的数量，下面两行是为了测试，连接后端后要删除
    // var solution_array = [1, 2];
    // var no_solution_array = [1, 2];
    if (session_key_ != "" && user_id_ != "") {
      wx.request({
        header: {"Content-Type":"application/x-www-form-urlencoded"},
        url: app.globalData.domain + "/getAnswersBrief",
        method: "GET",
        data: {
          session_key: wx.getStorageSync('sessionKey'),
          user_id: wx.getStorageSync('userId')
        },
        success(res) {
          let state=res.data.responseCode;
          if(state==200){
            var solution_array = res.data.data;
            that.setData({
              soultionNum:solution_array.length
            });
          }
          else{
            app.dealErrMes(state);
          }
          
        },
        fail(res) {
          wx.showToast({
            icon: "error",
            title: '网络繁忙！',
          });
        }
      });

      wx.request({
        header: {"Content-Type":"application/x-www-form-urlencoded"},
        url: app.globalData.domain + "/GetQuestions",
        method: "GET",
        data: {
          session_key: wx.getStorageSync('sessionKey')
        },
        success(res) {
          let state=res.data.responseCode;
          if(state==200){
            var no_solution_array = res.data.data;
            that.setData({
              noSolutionNum:no_solution_array.length
            });
          }
          else{
            app.dealErrMes(state);
          }
          
        },
        fail(res) {
          wx.showToast({
            icon: "error",
            title: '网络繁忙！',
          });

        }
      });
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})